package start.fly.test.database;

import java.sql.SQLException;
import java.util.Date;

public class control_user {
	
	//用户登录时验证用户名密码和角色
	public boolean select_user(String username,String password){
		linksql ls = new linksql();
		ls.link();
		try {
			ls.rs1 = ls.st.executeQuery("select * from user where username='"+username+"' and pwd='"+password+"'");
			if(ls.rs1.next()){
				return true;
			}else{
				return false;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}
	//用户登录时验证用户名身份
		public boolean select_userByShenfen(String username,String shenfen){
			linksql ls = new linksql();
			ls.link();
			try {
				ls.rs1 = ls.st.executeQuery("select * from user where username='"+username+"' and shenfen='"+shenfen+"'");
				if(ls.rs1.next()){
					return true;
				}else{
					return false;
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;
			
		}
	//判断用户是否存在
	public boolean select_user1(String username){
		linksql ls = new linksql();
		ls.link();
		try {
			ls.rs1 = ls.st.executeQuery("select * from user where username='"+username+"'");
			if(ls.rs1.next()){
				return true;
			}else{
				return false;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	//验证邮箱是否相同
	public boolean select_user2(String email){
		linksql ls = new linksql();
		ls.link();
		try {
			ls.rs1 = ls.st.executeQuery("select * from user where email='"+email+"'");
			if(ls.rs1.next()){
				return true;
			}else{
				return false;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	//将注册信息入库
	public void insert_user(String username,String password,String question,String answer,String safecode,String img,String email,String GroupNumber,String registertime){
		linksql ls = new linksql();
		ls.link();
		try {
			ls.rs = ls.st.executeUpdate("insert into user (username,pwd,question,answer,safecode,email,img,GroupNumber,shenfen,logintime,registertime) values('"+username+"','"+password+"','"+question+"','"+answer+"','"+safecode+"','"+email+"','"+img+"','"+GroupNumber+"','0','0','"+registertime+"')");
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//更新用户登录的次数
	public void update_logintime(String username){
		linksql ls = new linksql();
		ls.link();
		try {
			ls.rs = ls.st.executeUpdate("update user set logintime=logintime+1 where username='"+username+"'");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
